﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Net;

namespace RedditViewer
{
    class jsonSubredit
    {
        public string kind;
        public jsonSubreditData data;
        
    }
    class jsonSubreditData
    {
        public string modhash;
        public List<jsonPostWrapper> children;
        public string after { get; set; }
    }
    class jsonPostWrapper
    {
        public string kind;
        public jsonPost data;
    }

    public class jsonPost {
        
        public string url { get; set; }
        public string permalink { get; set; }
        public string title { get; set; }
        public int num_comments { get; set; }
        public string subreddit { get; set; }
        public int ups{ get; set; }
        public int downs { get; set; }
        public string author { get; set; }
        public string id { get; set; }
        public string domain { get; set; }
        public int score { get; set; }
        public double created_utc { get; set; }
        
    }

    class Subreddit
    {
        public string Name { get; set; }
        public List<Post> Posts { get; set; }
        public int PostLoadCount = 25;
        public Subreddit(string name)
        {
            this.Name = name;
            Posts = new List<Post>();
        }
        

        public void Load()
        {
            string Url = "http://www.reddit.com/r/" + Name + "/.json?limit=" + PostLoadCount;

            if (Posts.Count > 0)
                Url += "&after=" + Posts[Posts.Count - 1].After;

            WebClient web = new WebClient();
            string json = web.DownloadString(Url);
            JavaScriptSerializer ser = new JavaScriptSerializer();
            jsonSubredit jsr = null;
            try
            {
                jsr = ser.Deserialize<jsonSubredit>(json);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Unable to load subreddit " + Name);
            }
            
            foreach (var c in jsr.data.children)
            {
                jsonPost p = c.data;
                Post p2 = new Post
                {
                    Author = p.author,
                    Downs = p.downs,
                    NumComments = p.num_comments,
                    PermaLink = p.permalink,
                    Subreddit = p.subreddit,
                    Title = p.title,
                    Ups = p.ups,
                    Url = p.url,
                    Id = p.id,
                    Domain = p.domain,
                    Score = p.score,
                    Created = p.created_utc,
                    After = jsr.data.after
                };
                if (p2.CanLoad)
                {
                    if (!Posts.Any(_ => _.Id == p2.Id))
                        Posts.Add(p2);
                }
            }
        }

        public Post GetPost(int index)
        {
            if (index >= Posts.Count)
            {
                Load();
            }
            if (index >= Posts.Count)
                return null;
            return Posts[index];
        }
    }
}
